﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ApCMS.Models;
using AP.Framework.Infrastructure.Repositories.IRepositories;
using AP.Framework.Utilities.MvcPager;
namespace ApCMS.Controllers
{
    public class ProductController : Controller
    {
        IProduct _product;
        IProductCategory _productCategory;
        public ProductController(IProduct product, IProductCategory productCategory)
        {
            _product = product;
            _productCategory = productCategory;
        }
        public ActionResult Index(int? page, int? proCategory, string SearchValue)
        {
            ViewBag.ProductCategoryList = _productCategory.GetAll().OrderBy(p => p.Sequence);
            var productlList = from t in _product.GetAll()
                               join t1 in _productCategory.GetAll() on t.ProductCategoryId equals t1.ID
                               orderby t.Sequence
                               select new ProductModels
                               {
                                   ID = t.ID,
                                   Name = t.Name,
                                   CategoryName = t1.Name,
                                   CategoryId = t1.ID,
                                   Price = t.Price,
                                   Photo = t.Photo,
                                   PhotoLarge = t.PhotoLarge,
                                   Description = t.Description,
                                   IsOpenPrice = t.IsOpenPrice,
                                   IsPublic = t.IsPublic,
                                   Sequence = t.Sequence,
                                   IsDisable = t.IsDisable,
                                   ModifyBy = t.ModifyBy,
                                   ModifyTime = t.ModifyTime
                               };
            if (proCategory > 0)
            {
                productlList = productlList.Where(p => p.CategoryId == proCategory);
            }
            if (!string.IsNullOrWhiteSpace(SearchValue))
            {
                productlList = productlList.Where(p => p.Name.Contains(SearchValue));
            }
            return View(productlList.AsQueryable().OrderBy(p => p.Sequence).ToPagedList(page ?? 1, 12));
        }
        public ActionResult ViewDetails(int? productId)
        {
            var product = from t in _product.GetAll()
                          join t1 in _productCategory.GetAll() on t.ProductCategoryId equals t1.ID
                          orderby t.Sequence
                          select new ProductModels
                          {
                              ID = t.ID,
                              Name = t.Name,
                              CategoryName = t1.Name,
                              CategoryId = t1.ID,
                              Price = t.Price,
                              Photo = t.Photo,
                              PhotoLarge = t.PhotoLarge,
                              Description = t.Description,
                              IsOpenPrice = t.IsOpenPrice,
                              IsPublic = t.IsPublic,
                              Sequence = t.Sequence,
                              IsDisable = t.IsDisable,
                              ModifyBy = t.ModifyBy,
                              ModifyTime = t.ModifyTime
                          };
            return View(product.Where(p => p.ID == productId).FirstOrDefault());
        }
    }
}
